package com.hospital.pet.love.lovepethospital.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.pet.love.lovepethospital.domain.Employee;
import com.hospital.pet.love.lovepethospital.domain.User;
import com.hospital.pet.love.lovepethospital.dto.employee.EmployeeAddDto;
import com.hospital.pet.love.lovepethospital.dto.employee.EmployeeUpdateDto;
import com.hospital.pet.love.lovepethospital.dto.employee.JobDepartmentDto;
import com.hospital.pet.love.lovepethospital.exp.ShawCustomException;
import com.hospital.pet.love.lovepethospital.mapper.UserMapper;
import com.hospital.pet.love.lovepethospital.resp.ResultCodeEnum;
import com.hospital.pet.love.lovepethospital.service.EmployeeService;
import com.hospital.pet.love.lovepethospital.mapper.EmployeeMapper;
import com.hospital.pet.love.lovepethospital.util.AuthenticationContextUtil;
import com.hospital.pet.love.lovepethospital.util.RoleJudgeUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author RoselleShaw
 * @description 针对表【employee】的数据库操作Service实现
 * @createDate 2024-04-19 20:21:22
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
        implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private UserMapper userMapper;


    @Override
    public Employee get() {
        RoleJudgeUtil.employeeJudge();
        return employeeMapper.selectOne(new LambdaQueryWrapper<Employee>()
                .eq(Employee::getPhone, AuthenticationContextUtil.get().getPhone()));
    }

    @Override
    public void update(EmployeeUpdateDto employeeupdateDto) {
        RoleJudgeUtil.employeeJudge();
        Employee employee = new Employee();
        employee.setEmployeeName(employeeupdateDto.getEmployeeName());
        employee.setEmployeeSex(employeeupdateDto.getEmployeeSex());
        employee.setEmployeeAge(employeeupdateDto.getEmployeeAge());
        employeeMapper.update(employee, new LambdaQueryWrapper<Employee>()
                .eq(Employee::getPhone, AuthenticationContextUtil.get().getPhone()));
    }

    @Override
    public Page<Employee> getByPage(Integer pageNum, Integer pageSize) {
        RoleJudgeUtil.adminJudge();
        return employeeMapper.selectByPage(new Page<>(pageNum, pageSize));
    }

    @Override
    public void delete(Integer id) {
        RoleJudgeUtil.adminJudge();
        Employee employee = employeeMapper.selectById(id);
        if (employee == null) {
            throw new ShawCustomException(ResultCodeEnum.EMPLOYEE_NOT_EXIST);
        }
        userMapper.delete(new LambdaQueryWrapper<User>()
                .eq(User::getPhone, employee.getPhone()));
        employeeMapper.deleteById(employee);
    }

    @Override
    public void add(EmployeeAddDto employeeAddDto) {
        RoleJudgeUtil.adminJudge();
        if (StrUtil.isEmpty(employeeAddDto.getPhone())) {
            throw new ShawCustomException(ResultCodeEnum.PHONE_PASSWORD_NULL);
        }
        if (userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getPhone, employeeAddDto.getPhone())) != null) {
            throw new ShawCustomException(ResultCodeEnum.PHONE_EXIST);
        }
        User user = new User();
        user.setPhone(employeeAddDto.getPhone());
        user.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        user.setRole("员工");
        userMapper.insert(user);
        Employee employee = new Employee();
        employee.setPhone(employeeAddDto.getPhone());
        employeeMapper.insert(employee);
    }

    @Override
    public void jobDepartment(JobDepartmentDto jobDepartmentDto) {
        RoleJudgeUtil.adminJudge();
        Employee employee = new Employee();
        employee.setEmployeeId(jobDepartmentDto.getId());
        employee.setJob(jobDepartmentDto.getJob());
        employee.setDepartment(jobDepartmentDto.getDepartment());
        if (employeeMapper.updateById(employee) == 0) {
            throw new ShawCustomException(ResultCodeEnum.EMPLOYEE_NOT_EXIST);
        }
    }

}




